﻿/*
 * SptlDbParameterCollection
 * 
 * initial implementation : 2.12.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;


namespace Petra.Gis.DataServices
{
  public class SptlDbParameterCollection : DbParameterCollection, IFeatureSetParameters
  {

    //protected Dictionary<string, object> xparams;
    public XCommandParameters xparams;


    public SptlDbParameterCollection()
    {
      //xparams = new Dictionary<string, object>();
      xparams = new XCommandParameters();
    }




    public override int Add(object value)
    {

      if (!(value is SptlDbParameter))
        throw new ArgumentException("invalid input of type " + value.GetType().ToString());

      xparams.Add(((SptlDbParameter)value).ParameterName, (SptlDbParameter)value);
      return xparams.Count - 1;
    }




    public override void Clear()
    {
      xparams.Clear();
    }



    public override int IndexOf(object value)
    {
      int counter = 0, result = -1;

      //if (xparams.ContainsValue(value))
      foreach (KeyValuePair<string, IFeatureSetParameter> p in xparams)
      {
        if (p.Equals(value))
          result = counter;

        counter++;
      }
      return result;
    }




    public override int Count
    {
      get { return xparams.Count; }
    }




    public override bool Contains(string value)
    {
      return xparams.ContainsKey(value);
    }




    public override bool Contains(object value)
    {
      return xparams.ContainsValue((SptlDbParameter)value);
    }




    IEnumerator IEnumerable.GetEnumerator()
    {
      return xparams.GetEnumerator();
    }






    public override System.Collections.IEnumerator GetEnumerator()
    {
      return xparams.GetEnumerator();
    }




    protected override DbParameter GetParameter(int index)
    {
      int counter = 0;
      SptlDbParameter result = null;
      foreach (KeyValuePair<string, IFeatureSetParameter> p in xparams)
      {
        if (index == counter)
          result = (SptlDbParameter)p.Value;
      }
      return result;
    }




    protected override DbParameter GetParameter(string parameterName)
    {
      return (SptlDbParameter)xparams[parameterName];
    }




    public override void Insert(int index, object value)
    {
      if (!(value is SptlDbParameter))
        throw new ArgumentException("invalid parameter " + value.GetType().ToString());

      this.SetParameter(((SptlDbParameter)value).ParameterName, ((SptlDbParameter)value));
    }




    public override void AddRange(Array values)
    {
      throw new NotImplementedException();
    }




    public override void CopyTo(Array array, int index)
    {
      throw new NotImplementedException();
    }



    public override void Remove(object value)
    {
      xparams.Remove(((SptlDbParameter)value).ParameterName);
    }




    protected override void SetParameter(int index, DbParameter value)
    {
      //this.SetParameter(value.ParameterName, value);
      throw new NotImplementedException();
    }



    protected override void SetParameter(string parameterName, DbParameter value)
    {
      if (parameterName != value.ParameterName)
        throw new ArgumentException("parameterName != value.ParameterName");

      xparams.Add(parameterName, (SptlDbParameter)value);
    }





    public override void RemoveAt(string parameterName)
    {
      xparams.Remove(parameterName);
    }




    public override void RemoveAt(int index)
    {
      this.RemoveAt(this.GetParameter(index).ParameterName);
    }




    public override int IndexOf(string parameterName)
    {
      int counter = 0, result = -1;
      foreach (KeyValuePair<string, IFeatureSetParameter> p in xparams)
      {
        if (parameterName == p.Key)
          result = counter;

        counter++;
      }
      return result;
    }




    public override bool IsReadOnly
    {
      get { return false; }
    }




    public override bool IsFixedSize
    {
      get { return false; }
    }




    public override bool IsSynchronized
    {
      get { return false; }
    }




    public override object SyncRoot
    {
      get { return null; }
    }




    #region IDictionary members


    public void Add(KeyValuePair<string, IFeatureSetParameter> val)
    {
      this.Add(val.Value);
    }




    /// <summary>
    /// Returns an enumerator that iterates through the 
    /// System.Collections.Generic.Dictionary<TKey,TValue>.
    /// </summary>
    /// <returns>A System.Collections.Generic.Dictionary<TKey,TValue>.Enumerator structure for the 
    /// System.Collections.Generic.Dictionary<TKey,TValue>.</returns>
    System.Collections.Generic.IEnumerator<KeyValuePair<string, IFeatureSetParameter>> System.Collections.Generic.IEnumerable<KeyValuePair<string, IFeatureSetParameter>>.GetEnumerator()
    {
      return xparams.GetEnumerator();
    }




    public bool Remove(string parameterName)
    {
      if (this.ContainsKey(parameterName))
      {
        this.RemoveAt(parameterName);
        return true;
      }
      else
        return false;

    }




    public bool ContainsKey(string parameterName)
    {
      return this.Contains(parameterName);
    }




    public bool Remove(KeyValuePair<string, IFeatureSetParameter> val)
    {
      if (this.Contains(val.Key))
      {
        this.Remove(val.Key);
        return true;
      }
      else
        return false;
    }




    public bool Contains(KeyValuePair<string, IFeatureSetParameter> val)
    { 
      return this.Contains(val.Value);
    }




    public void Add(string paramName, IFeatureSetParameter param)
    {
      this.Add(param);
    }



    public void CopyTo(KeyValuePair<string, IFeatureSetParameter>[] val, int index)
    {
      this.CopyTo(val, index);
    }




    public bool TryGetValue(string key, out IFeatureSetParameter val)
    {
      if (this.ContainsKey(key))
      {
        val = this.xparams[key];
        return true;
      }
      else
      {
        val = null;
        return false;
      }
    }




    /// <summary>
    /// Gets a collection containing the keys in the System.Collections.Generic.Dictionary<TKey,TValue>.
    /// </summary>
    public virtual System.Collections.Generic.ICollection<string> Keys
    {
      get
      {
        return this.xparams.Keys;
      }
    }





    /// <summary>
    /// Gets a collection containing the values in the System.Collections.Generic.Dictionary<TKey,TValue>.
    /// </summary>
    public virtual System.Collections.Generic.ICollection<IFeatureSetParameter> Values
    {
      get
      {
        return this.xparams.Values;
      }
    }



    /// <summary>
    /// Gets or sets the value associated with the specified key.
    /// </summary>
    /// <param name="key">The key of the value to get or set.</param>
    /// <returns>The value associated with the specified key.
    /// If the specified key is not found, 
    /// a get operation throws a System.Collections.
    /// Generic.KeyNotFoundException, and a set operation creates 
    /// a new element with the specified key.</returns>
    public IFeatureSetParameter this[string key]
    {
      get
      {
        return (IFeatureSetParameter)this.xparams[key];
      }
      set
      {
        this.xparams[key] = value;
      }
    }

    #endregion IDictionary members


  }
}
